

#import "Track+Provider.h"
#import <MediaPlayer/MediaPlayer.h>
#import "musicing_mode.h"

@implementation Track (Provider)

+ (void)load
{
  dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
    [self remoteTracks];
  });

  dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
    [self musicLibraryTracks];
  });
}

+ (NSArray *)remoteTracks
{
    static NSArray *tracks = nil;

    NSMutableArray *allTracks = [NSMutableArray array];
    
    musicing_mode * mode =[[musicing_mode alloc]init];
    
    if ([User_information conversion].SING_Data.count == 0 ) {
        
        Track *track = [[Track alloc] init];
        [track setArtist:@"CHUN"];
        [track setTitle:@"CHUNCHUN01"];
        [track setAudioFileURL:[NSURL URLWithString:@"http://7xp1hi.com2.z0.glb.qiniucdn.com/mp3%E9%99%88%E5%A5%95%E8%BF%85%20-%20%E9%99%AA%E4%BD%A0%E5%BA%A6%E8%BF%87%E6%BC%AB%E9%95%BF%E5%B2%81%E6%9C%88%20%5Bmqms2%5D.mp3"]];
        
        [allTracks addObject:track];
        
        Track *track1 = [[Track alloc] init];
        [track1 setArtist:@"CHUN2"];
        [track1 setTitle:@"CHUNCHUN02"];
        [track1 setAudioFileURL:[NSURL URLWithString:@"http://7xp1hi.com2.z0.glb.qiniucdn.com/%E7%9C%BC%E7%9D%9B%E4%BC%9A%E8%AF%B4%E8%AF%9D.mp3"]];
        
        [allTracks addObject:track1];

    }
    else
    {
        for (int i = 0; i<[User_information conversion].SING_Data.count ; i++) {
            
            mode = [User_information conversion].SING_Data[i];
            Track * track = [[Track alloc] init];
            
            NSString * music_name = [NSString stringWithFormat:@"%@",mode.name];
            NSString * music_singer = [NSString stringWithFormat:@"%@",mode.singerName];
            
            [track setArtist:music_name];
            [track setTitle :music_singer];
            
            if(strIsEmpty(mode.url)){
                
                NSString *url = [NSString stringWithFormat:@"%@",mode.accompanyUrl];
                
                url = [url stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
                
                [track setAudioFileURL:[NSURL URLWithString:url]];
                
                NSLog(@"accompanyUrl%@",mode.accompanyUrl);
                [allTracks addObject:track];
                
            }else
            {
               [track setAudioFileURL:[NSURL URLWithString:mode.url]];
                NSLog(@"%@",mode.url);
                [allTracks addObject:track];
            }
            
        }

    }
    
    tracks = [allTracks copy];

  return tracks;
}

+ (NSArray *)musicLibraryTracks
{
    
  static NSArray *tracks = nil;

  static dispatch_once_t onceToken;
  dispatch_once(&onceToken, ^{
    NSMutableArray *allTracks = [NSMutableArray array];
    for (MPMediaItem *item in [[MPMediaQuery songsQuery] items]) {
      if ([[item valueForProperty:MPMediaItemPropertyIsCloudItem] boolValue]) {
        continue;
      }

      Track *track = [[Track alloc] init];
      [track setArtist:[item valueForProperty:MPMediaItemPropertyArtist]];
      [track setTitle:[item valueForProperty:MPMediaItemPropertyTitle]];
      [track setAudioFileURL:[item valueForProperty:MPMediaItemPropertyAssetURL]];
      [allTracks addObject:track];
    }

    for (NSUInteger i = 0; i < [allTracks count]; ++i) {
        
      NSUInteger j = arc4random_uniform((u_int32_t)[allTracks count]);
      [allTracks exchangeObjectAtIndex:i withObjectAtIndex:j];
    
    }

    tracks = [allTracks copy];
      
  });

  return tracks;
}

@end
